package cn.jbit.ams.user.controller;

import java.util.List;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.node.IntNode;
import com.github.pagehelper.Page;

import cn.jbit.ams.mainperson.biz.MainpersonService;
import cn.jbit.ams.order.biz.OrderService;
import cn.jbit.ams.user.biz.UserService;
import cn.jbit.ams.user.pojo.Mainperson;
import cn.jbit.ams.user.pojo.Order;
import cn.jbit.ams.user.pojo.Product;
import cn.jbit.ams.user.pojo.User;

@Controller
public class UserController {

	@Autowired
	private OrderService orderService;

	@Autowired
	private MainpersonService mainpersonService;

	@Autowired
	private UserService userService;


	/**
	 * 进入登录页面
	 * @return
	 */
	@RequestMapping(value= {"/","/login"})
	public String login() {
		return "login-page";
	}

	/**
	 * 进入新增页面
	 * @return
	 */
	@RequestMapping("goinsert")
	public String goinsert() {
		return "insert";
	}
	
   @RequestMapping("/gotu")
   public String tu() {
	   return "tu";
   }

	private int into=0;

	private int states=0;

	/**
	 * 登录查询所有基本信息
	 * @param user
	 * @param pageNum
	 * @param model
	 * @return
	 */
	@RequestMapping("/dologin")
	public String dologin(User user,Integer pageNum,Model model,String state) {
		if(state==null) {
			
		}else {
			states=Integer.parseInt(state);
			}
		into=into+2; 	
		//减少连接数据库的次数
		if(user==null||"".equals(user.getPassword())||"".equals(user.getUserName())) {
			return "login-page";
		}
		// 获取Subject主体对象
		Subject loginuser = SecurityUtils.getSubject();
		loginuser.logout();
		// 判断是否有认证信息
		if (!loginuser.isAuthenticated()) {
			// 创建令牌 将帐号密码存入令牌中
			UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(), user.getPassword());
			try {
				// 做认证校验
				loginuser.login(token);

			} catch (Exception e) {
				System.out.println(e.getMessage());
				return "login-page";
			}
		}
		if(pageNum==null) {
			pageNum=1;
		}
		System.out.println(into);
		if(into==2) {
			System.out.println("查询order");
			//查看订单信息
			Page<Order> orderPage=orderService.findOrder(pageNum);
			//查看维修人员人员信息
			List<Mainperson> mainperson=mainpersonService.optionMainperson();
			//查看用户信息
			Page<User> userPage=userService.findUser(pageNum,"");
			int countPage =userService.countPage();
			Page<Mainperson> mainPage=mainpersonService.findMainByPage(pageNum);
			//查询维修类型赋值给维修人员修改中的下拉框
			List<Product> productList=userService.getProduct();
			model.addAttribute("productList", productList);
			//添加维修人员到model
			model.addAttribute("mainPage", mainPage);
			//添加用户信息也页数到model
			model.addAttribute("countPage", countPage);
			//添加订单信息到model
			model.addAttribute("orderPage", orderPage);
			//添加用户信息到model
			model.addAttribute("userPage", userPage);
			//查看维修人员集合添加到model
			model.addAttribute("mainperson", mainperson);
			return "index";
		}else {
			System.out.println("不查询order");

			Page<Order> orderPage=orderService.getoptionOrder(states, pageNum);
			model.addAttribute("orderPage", orderPage);
			//查看维修人员人员信息
			List<Mainperson> mainperson=mainpersonService.optionMainperson();
			//查看用户信息
			Page<User> userPage=userService.findUser(pageNum,"");
			int countPage =userService.countPage();
			Page<Mainperson> mainPage=mainpersonService.findMainByPage(pageNum);
			//查询维修类型赋值给维修人员修改中的下拉框
			List<Product> productList=userService.getProduct();
			model.addAttribute("productList", productList);
			//添加维修人员到model
			model.addAttribute("mainPage", mainPage);
			//添加用户信息也页数到model
			model.addAttribute("countPage", countPage);
			//添加用户信息到model
			model.addAttribute("userPage", userPage);
			//查看维修人员集合添加到model
			model.addAttribute("mainperson", mainperson);
			return "index";
		}
	}

	/**
	 * 新增用户
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/insert")
	public String insert(User user) {
		//获取到密码
		String password = user.getPassword();
		//密码加密
		Object pwd2 = new SimpleHash("md5", password, user.getUserName(), 10);
		//重新添加到对象中覆盖原先的密码
		user.setPassword(pwd2.toString());
		//进行新增操作
		userService.insert(user);
		//返回到登录页面
		return "login-page";
	}

	/**
	 * 查询用户信息
	 * @param pageNum  页数
	 * @param model  
	 * @param userName  查询用户框中的值
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/findUserList")
	public String findUserList(Integer pageNum,Model model,String userName) {
		//查询到用户信息
		Page<User> userPage=userService.findUser(pageNum,userName);
		//将userPage添加到Model作用域中
		model.addAttribute("userPage", userPage);
		String userPageList=JSON.toJSONString(userPage);
		//将JSON返回到Ajax中
		return userPageList;
	}


	/**
	 * 查询订单信息
	 * @param pageNum
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/findOrderList")
	public String findOrderList(Integer pageNum,Model model) {
		//		if (pageNum == null) { pageNum = 1; }
		//查询订单信息
		Page<Order> orderPage=orderService.findOrder(pageNum);
		//添加orderPage到作用域中
		model.addAttribute("orderPage", orderPage);
		String userPageList=JSON.toJSONString(orderPage);
		//将查询出的订单信息返回到前端Ajax中显示数据
		return userPageList;
	}

	/**
	 * 修改用户信息
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getUser")
	public String getUser(Integer userId) {
		//获取到用户信息  将用户显示在文本框中  可查看 ,修改
		User user=userService.getUser(userId);
		String userString=JSON.toJSONString(user);
		//返回user对象到Ajax中显示数据
		return userString;
	}

	/**
	 * 修改维修人员
	 * @param user
	 * @param pageNum
	 * @param userName
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateUser")
	public String updateUser(User user,Integer pageNum,String userName,Model model) {
		//调用修改用户的方法
		userService.updateUser(user);
		//查询修改后的数据到页面进行刷新
		Page<User> userPage=userService.findUser(pageNum,userName);
		//添加查询到的数据到Model作用域中
		model.addAttribute("userPage", userPage);
		String userPageList=JSON.toJSONString(userPage);
		return userPageList;
	}
}
